﻿using System;
using ShureValidation.Language;

namespace ShureValidation.Rules
{
    /// <summary>
    /// Validates that a string only contains letter, digits and any of: <![CDATA['"\<>|-_.:,;'^~¨'*!#¤%&/()=?`´+}][{€$££@§½ ]]>
    /// </summary>
    /// <remarks>
    /// <para>
    /// Language file items:
    /// <list type="table">
    ///     <listheader>
    ///         <term>ItemName</term>
    ///         <description>Language text</description>
    ///     </listheader>
    ///     <item>
    ///         <term>AlphaNumeric</term>
    ///         <description>'{0}' may only contain alpha numeric letters.</description>
    ///     </item>
    /// </list>
    /// </para>
    /// </remarks>
    [LanguagePrompt("AlphaNumeric", "'{0}' may only contain alpha numeric characters.")]
    [Serializable]
    public class AlphaNumericRule : LettersAndDigitsRule
    {
        private const string ValidChars = "'\"\\<>|-_.:,;'^~¨'*!#¤%&/()=?`´+}][{€$££@§½€ ";

        /// <summary>
        /// Initializes a new instance of the <see cref="AlphaNumericRule"/> class.
        /// </summary>
        public AlphaNumericRule()
            : this(ValidChars)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AlphaNumericRule"/> class.
        /// </summary>
        /// <param name="extraCharacters">The extra characters.</param>
        public AlphaNumericRule(string extraCharacters)
            : base(extraCharacters + ValidChars)
        {
        }

        /// <summary>
        /// Format an error message
        /// </summary>
        /// <param name="fieldName">Field (have already been translated)</param>
        /// <param name="rulesLanguage">Language prompts for validation rules</param>
        /// <returns>
        /// Error message formatted for the current language.
        /// </returns>
        public override string Format(string fieldName, IModelLanguage rulesLanguage)
        {
            return string.Format(rulesLanguage["AlphaNumeric"], fieldName);
        }
    }
}